package com.zeroup.matrix.download;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.Toast;

import androidx.core.content.FileProvider;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.blankj.utilcode.util.DeviceUtils;
import com.zeroup.matrix.VediosActivity;
import com.zeroup.matrix.appplication.ZeroneAppliction;
import com.zeroup.matrix.bean.AjaxResult;
import com.zeroup.matrix.bean.MGanVideo;
import com.zeroup.matrix.utile.DouyinUtiles;

import org.kymjs.kjframe.KJHttp;
import org.kymjs.kjframe.http.HttpCallBack;
import org.kymjs.kjframe.http.HttpParams;
import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.FileUtils;
import org.kymjs.kjframe.utils.KJLoger;

import java.io.File;

public class ShareVedioDownLoad implements VedioDownLoad{

    VediosActivity activity;

    public ShareVedioDownLoad(VediosActivity activity) {
        this.activity = activity;
    }

    public void clearGallery(Context context) {
        Uri externalContentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
        Cursor cursor = context.getContentResolver().query(externalContentUri, null, null, null, null);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                // 获取图片的ID
                String id = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID));
                // 删除图片
                context.getContentResolver().delete(
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                        MediaStore.Video.Media._ID + "=?",
                        new String[]{id}
                );
            }
            cursor.close();
        }
    }

    @Override
    public void download(MGanVideo mGanVideo) {
        String name = mGanVideo.getFileName()+"_"+AndroidDownloadManager.getFileNameByUrl(mGanVideo.getVideoThirdUrl());
        new Thread(() -> new AndroidDownloadManager(activity, mGanVideo.getVideoThirdUrl(),name)
                .setListener(new AndroidDownloadManagerListener() {
                    @Override
                    public void onPrepare() {
                        Log.d("downloadVideo", "onPrepare");
                    }

                    @Override
                    public void onSuccess(String path) {
//                        Toast.makeText(activity, "视频已保存到相册", Toast.LENGTH_SHORT).show();
//                        SaveUtils.saveVideoToAlbum(activity,path);
//                        SaveUtils.saveVideoToAlbum()
                        Log.d("downloadVideo", "onSuccess >>>>" + path);
                        DouyinUtiles.push(activity,mGanVideo,path);
                    }

                    @Override
                    public void onFailed(Throwable throwable) {
//                        Toast.makeText(activity, "视频下载失败，请重新下载！", Toast.LENGTH_SHORT).show();
                        Log.e("downloadVideo", "onFailed", throwable);
                    }
                }).download()).start();
        clearGallery(activity);
//        KJHttp kjh = new KJHttp();
//        HttpParams params = new HttpParams();
//        String sbh = DeviceUtils.getUniqueDeviceId();
//        params.put("sbh",sbh);
//        Log.d("shareSchema====sbh》{}" , sbh);
//        params.putHeaders("X-Access-Token",activity.getToken());
//        kjh.get(ZeroneAppliction.apiUrl + "/video/mGanVideo/shareSchema",params ,new HttpCallBack() {
//            @Override
//            public void onLoading(long count, long current) {
//                super.onLoading(count, current);
//            }
//
//            @Override
//            public void onPreStart() {
//            }
//
//            @Override
//            public void onSuccess(String o) {
//                AjaxResult ajaxResult = JSON.parseObject(o, AjaxResult.class);
//                if(ajaxResult.isSuccess()){
//                    String re = (String) ajaxResult.getResult();
//                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse( re));
//                    activity.startActivity(intent);
//                }else{
//                    Toast.makeText(activity, ajaxResult.getMessage(), Toast.LENGTH_SHORT).show();
//                }
//            }
//
//            @Override
//            public void onFailure( int errorNo, String strMsg) {
//                Toast.makeText(activity, strMsg, Toast.LENGTH_SHORT).show();
//            }
//            @Override
//            public void onFinish() {
//
//            }
//        });
    }
}
